﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PersonalExpenseTracker.PersistenceLayer.Xml
{
    public class Repository
    {
        /// <summary>
        /// Returns the data xml file path.
        /// </summary>
        public string DataFilePath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + "\\data\\expenses.xml";
            }
        }

        /// <summary>
        /// Returns the data xml file path.
        /// </summary>
        public string DataDirectory
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + "\\data";
            }
        }

        /// <summary>
        /// Loads expense datastore data set from the data xml file.
        /// </summary>
        public DataStore LoadDataStore()
        {
            if (System.IO.File.Exists(this.DataFilePath))
            {
                DataStore ds = new DataStore();
                ds.ReadXml(this.DataFilePath);
                ds.AcceptChanges();
                return ds;
            }
            else
            {
                return new DataStore();
            }
        }

        /// <summary>
        /// Updates expense datastore dataset and stores its information to xml file.
        /// </summary>
        public bool UpdateDataStore(DataStore ds)
        {
            if (ds.HasChanges())
            {
                if (!System.IO.Directory.Exists(DataDirectory))
                {
                    System.IO.Directory.CreateDirectory(DataDirectory);
                }
                ds.WriteXml(this.DataFilePath);
                ds.AcceptChanges();
                return true;
            }
            return false;
        }

    }
}
